package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjFilterDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperFilterDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperSortDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiFilterChangerService;
import com.mysoft.gfyx.marketdevelop.service.OpenedTradingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OpenedTradingServiceImp extends BaseDataApiServiceImp implements OpenedTradingService {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;


    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("uuid");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "developer_opened_trading";
        fieldsSetting.getFields().add(new DataApiField("uuid", groupName, "RowID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("sell_project_id", groupName, "公司项目ID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("uid", groupName, "项目ID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("developer_name", groupName, "开发商名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("sub_developer_name", groupName, "开发商名称(子公司)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "社会统一信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("project_title", groupName, "项目名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("province", groupName, "区域（省）", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("province_code", groupName, "区域（省）Code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("city", groupName, "区域（市）", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("city_code", groupName, "区域（市）Code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("address", groupName, "地址", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("opening_time", groupName, "开盘时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("delivery_time", groupName, "竣工时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("decorate", groupName, "交付方式", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("fitment_fee", groupName, "交付方式", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("property_category", groupName, "项目类型", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("land_date", groupName, "拿地日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("price", groupName, "单价（元）", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("new_house_deal_num", groupName, "已售总套数", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("new_house_deal_money", groupName, "已售总金额", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("new_house_sale_rate", groupName, "去化率", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadData(LoadDataParams options) {
        String creditCode = options.getOptions().getContext().get("id").toString();
        GetDeveloperFilterDataRequestDto dto = new GetDeveloperFilterDataRequestDto();

        dto = developerService.setCommonData(dto);
        dto.setDetail_table_name(DeveloperService.TABLE_OPENED_TRADING_WITH_INVESTMENT);
        dto.setPage(options.getPageIndex() + 1);
        dto.setPage_size(options.getPageSize());
        //dto.addFilterDtos("organizing_code", BaseTjFilterDto.OPERATE_EQUAL, creditCode, DeveloperService.TABLE_OPENED_TRADING, BaseTjFilterDto.ASSOCIATE_NONE);

        List<Filter> filters = options.getOptions().getFilters();
        FilterProvince(filters);
        loadDataApiCommonService.setDefaultDeveloperWithoutInvestment(filters, creditCode);
        loadDataApiCommonService.filterChange(filters, getFilterChanger());
        dto.addFilterByLoadDataApi(filters, DeveloperService.TABLE_OPENED_TRADING_WITH_INVESTMENT);

        // 排序
        ArrayList<GetDeveloperSortDto> sorts = new ArrayList<>();
        GetDeveloperSortDto sort = new GetDeveloperSortDto();
        sort.setSort_type(GetDeveloperSortDto.SORT_DESC);
        sort.setAttribute_name("opening_time");
        sort.setTable_name(DeveloperService.TABLE_OPENED_TRADING_WITH_INVESTMENT);
        sorts.add(sort);
        dto.setSorts(sorts);

        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(dto);

        return developerService.getTjResFromLoadData(res);
    }

    public void FilterProvince(List<Filter> filters) {
        if (filters.size() > 0) {
            for (int i = 0; i < filters.size(); i++) {
                if (filters.get(i).getCondition().size() > 0 && filters.get(i).getCondition().get(0).getField().equals("province_code") && filters.get(i).getCondition().get(0).getValue().equals("0")) {
                    filters.remove(i);
                }
            }
        }
    }

    private class FilterChanger implements LoadDataApiFilterChangerService {
        @Override
        public void change(SqlCondition condition) {
            if ("property_category".equals(condition.getField())) {
                condition.setOperatorType("like");
            }
        }
    }

    LoadDataApiFilterChangerService getFilterChanger() {
        return new FilterChanger();
    }

}
